package com.example.work_study_system.web.controller;



import com.example.work_study_system.common.R;
import com.example.work_study_system.common.utls.JwtUtil;
import com.example.work_study_system.model.domain.User;
import com.example.work_study_system.model.dto.LoginDTO;
import com.example.work_study_system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody LoginDTO dto) {
        User user = userService.login(dto.getUsername(), dto.getPassword());
        if (user == null) {
            return R.error("用户名或密码错误");
        }
        String token = JwtUtil.createToken(user.getId(), user.getRole());
        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        data.put("role", user.getRole());
        data.put("username", user.getUsername());
        return R.ok(data);
    }
} 